package com.zhongshengnetwork.rightbe.common;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.graphics.Paint.Cap;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Join;
import android.graphics.Paint.Style;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;

public class VerTextView extends View {
    public final int RIGHT = 1;
    public static final int LEFT = 0;
    public int MaxInOneLine = 0;
    private Paint paint;
    private Paint mPaint;
    private Path mPath;
    private int mTextPosX = 0;
    private int mTextPosY = 0;
    private int mTextWidth = 0;
    private int mTextHeight = 0;
    private int mFontHeight = 0;
    private float mFontSize = 40.0F;
    private String text = "";
    private int mLineSize = 0;
    private int mLineWidth = 0;
    private int mLineWidthWithoutUnderLine = 0;
    private int mLineSpacing = 20;
    private int TextLength = 0;
    private static int maxCountInOneLine = 18;
    private int oldwidth = 0;
    private Align textStartAlign;
    private boolean IsOpenUnderLine;
    private int underLineWidth;
    private int underLineColor;
    private int underLineSpacing;
    private Handler mHandler;
    private BitmapDrawable drawable;

    public VerTextView(Context context) {
        super(context);
        this.textStartAlign = Align.RIGHT;
        this.IsOpenUnderLine = false;
        this.underLineWidth = 5;
        this.underLineColor = -16777216;
        this.underLineSpacing = 10;
        this.mHandler = null;
        this.drawable = (BitmapDrawable)this.getBackground();
        this.paint = new Paint();
        //this.paint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/fun_font.TTF"));
        this.paint.setTextAlign(Align.CENTER);
        this.paint.setAntiAlias(true);
        this.paint.setColor(-16777216);
        if (this.IsOpenUnderLine) {
            this.mPath = new Path();
            this.mPaint = new Paint();
            this.mPaint.setColor(this.underLineColor);
            this.mPaint.setAntiAlias(true);
            this.mPaint.setDither(true);
            this.mPaint.setStyle(Style.STROKE);
            this.mPaint.setStrokeJoin(Join.ROUND);
            this.mPaint.setStrokeCap(Cap.ROUND);
            this.mPaint.setStrokeWidth((float)this.underLineWidth);
        }

        this.mFontSize = 40.0F;
    }

    public VerTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.textStartAlign = Align.RIGHT;
        this.IsOpenUnderLine = false;
        this.underLineWidth = 5;
        this.underLineColor = -16777216;
        this.underLineSpacing = 10;
        this.mHandler = null;
        this.drawable = (BitmapDrawable)this.getBackground();
        this.paint = new Paint();
        //this.paint.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/fun_font.TTF"));
        this.paint.setTextAlign(Align.CENTER);
        this.paint.setAntiAlias(true);
        this.paint.setColor(-16777216);
        if (this.IsOpenUnderLine) {
            this.mPath = new Path();
            this.mPaint = new Paint();
            this.mPaint.setColor(this.underLineColor);
            this.mPaint.setAntiAlias(true);
            this.mPaint.setDither(true);
            this.mPaint.setStyle(Style.STROKE);
            this.mPaint.setStrokeJoin(Join.ROUND);
            this.mPaint.setStrokeCap(Cap.ROUND);
            this.mPaint.setStrokeWidth((float)this.underLineWidth);
        }

        try {
            this.mFontSize = Float.parseFloat(attrs.getAttributeValue((String)null, "textSize"));
        } catch (Exception var4) {
            Log.e("get font size", "error");
            this.mFontSize = 40.0F;
        }

    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.draw(canvas, this.text);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int measuredHeight = this.measureHeight(heightMeasureSpec);
        if (this.mTextWidth == 0) {
            this.getTextSize();
        }

        Log.e("measuredHeight", measuredHeight + "");
        this.setMeasuredDimension(this.mTextWidth, Math.min(measuredHeight, this.MaxInOneLine));
    }

    private void draw(Canvas canvas, String text) {
        if (this.IsOpenUnderLine) {
            this.mPath = new Path();
            this.mPaint = new Paint();
            this.mPaint.setColor(this.underLineColor);
            this.mPaint.setAntiAlias(true);
            this.mPaint.setDither(true);
            this.mPaint.setStyle(Style.STROKE);
            this.mPaint.setStrokeJoin(Join.ROUND);
            this.mPaint.setStrokeCap(Cap.ROUND);
            this.mPaint.setStrokeWidth((float)this.underLineWidth);
        }

        this.mTextPosY = 0;
        this.mTextPosX = this.textStartAlign == Align.LEFT ? this.mLineWidth : this.mTextWidth - this.mLineWidth;

        for(int i = 0; i < this.TextLength; ++i) {
            char StringItem = text.charAt(i);
            if (StringItem == '\n') {
                if (this.IsOpenUnderLine) {
                    this.drawUnderLine(canvas);
                }

                if (this.textStartAlign == Align.LEFT) {
                    this.mTextPosX += this.mLineWidth;
                } else {
                    this.mTextPosX -= this.mLineWidth;
                }

                this.mTextPosY = 0;
            } else {
                this.mTextPosY += this.mFontHeight;
                if (this.mTextPosY > this.mTextHeight) {
                    if (this.IsOpenUnderLine) {
                        this.drawUnderLine(canvas);
                    }

                    if (this.textStartAlign == Align.LEFT) {
                        this.mTextPosX += this.mLineWidth;
                    } else {
                        this.mTextPosX -= this.mLineWidth;
                    }

                    --i;
                    this.mTextPosY = 0;
                } else {
                    canvas.drawText(String.valueOf(StringItem), (float)this.mTextPosX, (float)this.mTextPosY, this.paint);
                    if (i == this.TextLength - 1 && this.IsOpenUnderLine) {
                        this.drawUnderLine(canvas);
                    }
                }
            }
        }

    }

    private void drawUnderLine(Canvas canvas) {
        this.mPath.moveTo((float)(this.mTextPosX - this.mLineWidthWithoutUnderLine / 2 - this.underLineSpacing), 0.0F);
        this.mPath.lineTo((float)(this.mTextPosX - this.mLineWidthWithoutUnderLine / 2 - this.underLineSpacing), (float)this.mTextPosY);
        canvas.drawPath(this.mPath, this.mPaint);
        this.mPath.reset();
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        float[] widths = new float[1];
        this.paint.getTextWidths("蛤", widths);
        int result = (int)widths[0] * maxCountInOneLine;
        if (specMode == 1073741824) {
            result = specSize;
        } else if (specMode == -2147483648) {
            result = Math.min(result, specSize);
        }

        this.mTextHeight = result;
        return result;
    }

    public void setText(String text) {
        this.text = text;
        this.TextLength = text.length();
        if (this.mTextHeight > 0) {
            this.getTextSize();
        }

    }

    private void getTextSize() {
        int SizeInOneLine = 0;
        this.paint.setTextSize(this.mFontSize);
        if (this.mLineWidth == 0) {
            float[] widths = new float[1];
            this.paint.getTextWidths("蛤", widths);
            this.mLineWidth = this.mLineSpacing + (int)widths[0];
            if (this.IsOpenUnderLine) {
                this.mLineWidthWithoutUnderLine = this.mLineWidth;
                this.mLineWidth += this.underLineWidth + this.underLineSpacing;
            }
        }

        FontMetrics fm = this.paint.getFontMetrics();
        this.mFontHeight = (int)(Math.ceil((double)(fm.descent - fm.top)) * 0.9D);
        this.mLineSize = 0;
        this.MaxInOneLine = 0;

        for(int i = 0; i < this.TextLength; ++i) {
            char StringItem = this.text.charAt(i);
            if (StringItem == '\n') {
                ++this.mLineSize;
                SizeInOneLine = 0;
            } else {
                SizeInOneLine += this.mFontHeight;
                if (SizeInOneLine > this.mTextHeight) {
                    ++this.mLineSize;
                    --i;
                    SizeInOneLine = 0;
                } else {
                    if (SizeInOneLine > this.MaxInOneLine) {
                        this.MaxInOneLine = SizeInOneLine;
                    }

                    if (i == this.TextLength - 1) {
                        ++this.mLineSize;
                    }
                }
            }
        }

        ++this.mLineSize;
        ++this.mLineSize;
        ++this.mLineSize;
        this.mTextWidth = this.mLineWidth * this.mLineSize;
        this.measure(this.mTextWidth, this.getHeight());
        this.layout(this.getRight() - this.mTextWidth, this.getTop(), this.getRight(), this.getBottom());
    }

    public void setFontSize(float mFontSize) {
        if (mFontSize != this.paint.getTextSize()) {
            this.mFontSize = mFontSize;
            if (this.mTextHeight > 0) {
                this.getTextSize();
            }
        }

    }

    public void setmHandler(Handler mHandler) {
        this.mHandler = mHandler;
    }

    public void setmLineSpacing(int mLineSpacing) {
        this.mLineSpacing = mLineSpacing;
    }

    public static void setMaxCountInOneLine(int maxCountInOneLine) {
        VerTextView.maxCountInOneLine = maxCountInOneLine;
    }

    public void setTextStartAlign(int leftOrRight) {
        switch(leftOrRight) {
            case 0:
                this.textStartAlign = Align.LEFT;
                break;
            case 1:
                this.textStartAlign = Align.RIGHT;
        }

    }

    public void setTextColor(int color) {
        this.paint.setColor(color);
    }

    public void setIsOpenUnderLine(boolean isOpenUnderLine) {
        this.IsOpenUnderLine = isOpenUnderLine;
    }

    public void setUnderLineWidth(int underLineWidth) {
        this.underLineWidth = underLineWidth;
    }

    public void setUnderLineColor(int underLineColor) {
        this.underLineColor = underLineColor;
    }

    public void setUnderLineSpacing(int underLineSpacing) {
        this.underLineSpacing = underLineSpacing;
    }
}

